package com.mingmen.canting.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.canting.R;
import com.mingmen.canting.defineview.GlideRoundTransform;
import com.mingmen.canting.httpconfig.AppConfig;
import com.mingmen.canting.model.CommentModel;

import java.util.ArrayList;
import java.util.List;

public class CommentDetailAdapter extends RecyclerView.Adapter<CommentDetailAdapter.MyHolder> {
    private Context context;
    private List<CommentModel.DataBean.DetailsBean> data;

    private OnChildClick onChildClick;

    public CommentDetailAdapter(Context context) {
        this.context = context;
        this.data = new ArrayList<>();
    }

    public void setData(List<CommentModel.DataBean.DetailsBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void setOnChildClick(OnChildClick onChildClick) {
        this.onChildClick = onChildClick;
    }


    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_comment_detail, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        CommentModel.DataBean.DetailsBean detailsBean = data.get(position);
        Glide.with(context).load(AppConfig.IMGURL + detailsBean.getPicture()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(context, 5)).into(holder.ivAvatar);
    }


    @Override
    public int getItemCount() {
        return data.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        ImageView ivAvatar;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            imageView = itemView.findViewById(R.id.item_two_iv);
        }

        public void buildHolder(final int position) {
/*            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.ic_launcher)//图片加载出来前，显示的图片
                    .fallback(R.mipmap.a) //url为空的时候,显示的图片
                    .error(R.mipmap.b);//图片加载失败后，显示的图片 图片加载失败


            Glide.with(context).load(data.get(position))
                    .apply(options)
                    .into(imageView);*/

//            Glide.with(context).load(data.get(position)).into(imageView);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onChildClick != null) {
                      /*  if (!ButtonUtils.isFastDoubleClick()) {
                            onChildClick.onItemClick(position);
                        }*/
                        onChildClick.onItemClick(position);
                    }
                }
            });

        }
    }

    public interface OnChildClick {
        void onItemClick(int position);
    }
}
